package xhr03;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {

    /**
    * @Description: 消费者接收消息
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/8/16 15:10
    */
    public static void main(String[] args) throws Exception {
        // 1.创建消费者
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("consumer_hello_group");
        // 2.设置NameServer地址,均衡调用对象
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        // 3.设置订阅指定标签
        defaultMQPushConsumer.subscribe("hello_topic2", "*");
        // 4.注册监听器消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt msg : list) {
                    System.out.println(new String(msg.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 5.启动消费者
        defaultMQPushConsumer.start();
        System.out.println("消费者启动...");
    }

}
